<?php
defined('IN_TS') or die('Access Denied.');
switch($ts){

	//确认完成
	case "attend":

		$js = intval($_GET['js']);

    	$userid = intval($TS_USER['userid']);

		if($userid == 0) {
			getJson('请登录后再操作！',1);
		}

		$taskid = intval($_POST['taskid']);
		$memberid = intval($_POST['memberid']);
		$strTask = $new['task']->find('task',array(
			'taskid'=>$taskid,
		));

		if($userid<>$strTask['userid']&&$GLOBALS['TS_USER']['isadmin']==0){
			getJson('您没有操作权限！',1);
		}

		$taskUser = $new['task']->find('task_users',array(
			'taskid'=>$taskid,
			'userid'=>$memberid,
		));

		if(!$taskUser){
			getJson('此人不在认领名单中',1);
		}


		//标记他完成了
		$new['task']->update('task_users',array(
			'taskid'=>$taskid,
			'userid'=>$memberid,
		),array(
			'status'=>1,
		));

		$term = $strTask['timelong']*$taskUser['quota'];

		// 对服务时长进行处理
		$arr=array('groupid' => 1,
			'userid' => $memberid,
			'leaderid'=> $strTask['userid'],
			'begintime' => $taskUser['addtime'],
			'day' => date('ymd',$taskUser['addtime']),
			'endtime' => time(),
			'period' => $term,
			'term' =>$term,
			'cateid' => 4,
			'addtime' => time(),
			'taskid' => $taskid,
			);



		 $dutyid=aac('duty')->create('duty', $arr);


		$sql = "select sum(term) as sums from ts_duty where userid=".$memberid." and status=1";
		$arr=$db->once_fetch_assoc($sql);
		$count_duty=$arr['sums'];


		aac('user')->update('user_info',array(
			'userid'=>$memberid,
		),array(
			'count_duty'=>$count_duty,'lasttime'=>$taskUser['addtime']
		));

		//统计一下达成的
		$sql = "select sum(quota) as sums from ts_task_users where taskid=".$taskid." and status=1";
		$arr=$db->once_fetch_assoc($sql);
		$userDoNum=$arr['sums'];

		$new['task']->update('task',array(
			'taskid'=>$taskid,
		),array(
			'count_userdo'=>$userDoNum
		));

		$arrUser = aac('user')->find('user_info',array('userid'=>$memberid));
		//发一条广播
		$content = '完成了任务<a href="'.tsUrl('task','show',array('id'=>$taskid)).'">'.$strTask['title'].'！</a>';
		aac('weibo')->create('weibo',array(
			'userid'=>$memberid,
			'content'=>$content,
			'isaudit'=>0,
			'addtime'=>date('Y-m-d H:i:s'),
			'uptime'=>date('Y-m-d H:i:s'),
		));

		if($userid!= $memberid) {


			//发送消息给任务认领者
			$msg_userid = $userid;
			$msg_touserid = $memberid;
			$msg_content = '我已确认你完成了任务《' . $strTask['title'] . '》，服务记录会自动生成，不需要手工登记！';
			$msg_tourl = tsUrl('duty', 'show', array('dutyid' => $dutyid));
			aac('message')->sendmsg($msg_userid, $msg_touserid, $msg_content, $msg_tourl);

		}


		echo '1';

		break;

	//确认没完成
	case "absent":

		$js = intval($_GET['js']);
		$userid = intval($TS_USER['userid']);

		if($userid == 0) {
			getJson('请登录后再操作！',1);
		}

		$taskid = intval($_POST['taskid']);
		$memberid = intval($_POST['memberid']);
		$strTask = $new['task']->find('task',array(
			'taskid'=>$taskid,
		));

		if($userid<>$strTask['userid']&&$GLOBALS['TS_USER']['isadmin']==0){
			getJson('您没有操作权限！',1);
		}

		$taskUser = $new['task']->find('task_users',array(
			'taskid'=>$taskid,
			'userid'=>$memberid,
		));

		if(!$taskUser){
			getJson('此人不在认领名单中',1);
		}


		//标记他没取
		$new['task']->update('task_users',array(
			'taskid'=>$taskid,
			'userid'=>$memberid,
		),array(
			'status'=>0,
		));


		// 对服务时长进行处理
		aac('duty') -> delete('duty',array('taskid'=>$taskid,'userid'=>memberid));

		$sql = "select sum(term) as sums from ts_duty where userid=".$memberid." and status=1";
		$arr=$db->once_fetch_assoc($sql);
		$count_duty=$arr['sums'];

		$arrDuty = aac('duty') -> find('duty',array('userid'=>memberid,'','dutyid desc'));

		$new['user']->update('user_info',array(
			'userid'=>$memberid,
		),array(
			'count_duty'=>$count_duty,'lasttime'=>$arrDuty['begintime']
		));




		//统计一下达成的
		$sql = "select sum(quota) as sums from ts_task_users where taskid=".$taskid.' and status=1';
		$arr=$db->once_fetch_assoc($sql);
		$userDoNum=$arr['sums'];

		$new['task']->update('task',array(
			'taskid'=>$taskid,
		),array(
			'count_userdo'=>$userDoNum
		));


		//task
		$strTask = $new['task']->find('task',array(
			'taskid'=>$taskid,
		));

		echo '1';

		break;

	//取消认领任务
	case "cancel":

		$js = intval($_GET['js']);

		$userid = intval($TS_USER['userid']);
		
		if($userid == 0) {
			echo 0;exit;
		}
		
		$taskid = intval($_POST['taskid']);
		
		$new['task']->delete('task_users',array(
		
			'taskid'=>$taskid,
			'userid'=>$userid,
		
		));

		//统计一下认领的
		$sql = "select sum(quota) as sums from ts_task_users where taskid=".$taskid;
		$arr=$db->once_fetch_assoc($sql);
		$userTaskNum=$arr['sums'];
		


		$new['task']->update('task',array(
			'taskid'=>$taskid,
		),array(
			'count_userwish'=>$userTaskNum,
		));
		
		echo '1';
		
		break;
		
	//认领任务
	case "join":
	
		$js = intval($_GET['js']);
		
	
		$userid = intval($TS_USER['userid']);
		
		if($userid == 0) {
			getJson('请登录后再认领任务',$js);
		}
		
		$taskid = intval($_POST['taskid']);

        $strTask = $new['task']->find('task',array(
            'taskid'=>$taskid,
        ));

        if($strTask==''){
            getJson('不存在该任务！',$js);
        }



		$username = trim($_POST['username']);
		$phone = trim($_POST['phone']);
		$quota = trim($_POST['quota']);
		$groupid = intval($_POST['groupid']);

		if($username=='' || $phone=='' || $quota==''){
			getJson('姓名、手机号、认领数不能为空',$js);
		}
		if($strTask['limittime']<=time()){
			getJson('已过有效期',$js);
		}

		if($strTask['quota']>0&&$quota>$strTask['quota']-$strTask['count_userwish']){
			getJson('您认领的任务数大于当前的余额',$js);
		}



		//删除之前的认领信息
		$new['task']->delete('task_users',array(
			'taskid'=>$taskid,
			'userid'=>$userid,
		));
		
		
		$new['task']->create('task_users',array(
			'taskid'=>$taskid,
			'userid'=>$userid,
			'quota'=>$quota,
			'limittime'=>strtotime("+7 day"),
			'status'=>0,
			'addtime'=>time(),
		));

		//更新用户信息
		$new['task']-> update('user_info',array(
			'userid'=>$userid,
		),array(
			'username'=>$username,
			'phone'=>$phone,
		));


		//统计一下认领的
		$sql = "select sum(quota) as sums from ts_task_users where taskid=".$taskid;
		$arr=$db->once_fetch_assoc($sql);
		$userTaskNum=$arr['sums'];
		

		$new['task']->update('task',array(
			'taskid'=>$taskid,
		),array(
			'count_userwish'=>$userTaskNum
		));

		if($strTask['userid'] != $userid){
			$msg_userid = '0';
			$msg_touserid = $strTask['userid'];
			$msg_content = '你发布的任务《'.$strTask['title'].'》新增一个认领，快去看看吧^_^ ';
			$msg_tourl = tsUrl('task','show',array('id'=>$taskid));
			aac('message')->sendmsg($msg_userid,$msg_touserid,$msg_content,$msg_tourl);
		}


		//发一条便笺
		$content = '认领了任务《<a href="'.tsUrl('task','show',array('id'=>$taskid)).'">'.$strTask['title'].'》！</a>';
		aac('weibo')->create('weibo',array(
			'userid'=>$TS_USER['userid'],
			'content'=>$content,
			'isaudit'=>0,
			'addtime'=>date('Y-m-d H:i:s'),
			'uptime'=>date('Y-m-d H:i:s'),
		));


		getJson('任务认领成功，请在您期限内完成。',1,1);
		
		
		break;
		
}
